package com.luozongyao.sysInform.controller;


import com.luozongyao.commons.R;
import com.luozongyao.statistics.entity.User;
import com.luozongyao.statistics.service.UserService;
import com.luozongyao.sysInform.entity.Inform;
import com.luozongyao.sysInform.entity.vo.InformVo;
import com.luozongyao.sysInform.service.InformService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author luozongyao
 * @since 2021-11-14
 */
@Api(description="系统统计管理")
@RestController
@RequestMapping("/sysInform/inform")
@CrossOrigin
public class InformController {

    @Autowired
    private InformService informService;

    @Autowired
    private UserService userService;

    @ApiOperation(value = "发布系统消息")
    @PostMapping("sendSystemMessage")
    public R sendSystemMessage(@RequestBody InformVo informVo){
        String informTitle = informVo.getTitle();
        String informContent = informVo.getInitContent();
        List<User> list = userService.list(null);
        List<Inform> informList = new ArrayList<Inform>();
        for (User user : list) {
            Integer userId = user.getUserId();
            Inform newInform = new Inform();
            newInform.setInformedUserId(userId);
            newInform.setInformTitle(informTitle);
            newInform.setInformContent(informContent);
            informList.add(newInform);
        }
        boolean flag = informService.saveBatch(informList);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }


}

